package java101;

import javax.swing.JOptionPane;

public class CubicRoot {

	static String a;
	
	public static void main(String[] args) {

		a = JOptionPane.showInputDialog("Type in a cubic equation such that y = x^3 + a\n" 
				+ "where the cubic root of 'a' is an integer."); 
		
		if (a.contains("+")){
			
		aIncludesPlus();
			}
		
		if (a.contains("-")){
			
			aIncludesMinus();
			}
		
		else {
			JOptionPane.showMessageDialog(null, "Nope, you did it wrong, and I\n" 
					+ "don't know how to program.\nSo... bye.");
		}
		}
	
		
		
	
		public static void aIncludesPlus ()
		{
			String[] a1 = a.split("+");
			
		String SubA = a1[1].substring(1);
		int a2 = Integer.parseInt(SubA);
		double a3 = Math.cbrt(a2);
		double a4 = Math.pow(a3, 2);
				
			String b = String.format("The original equation is: %s\n" 
				+ "The factored equation is: (x + %.0f) (x^2 - %.0fx + %.0f)", a, a3, a3, a4);
			JOptionPane.showMessageDialog(null, b);
			
			String c = String.format("The root of the equation is: x = %.0f", a3);
			JOptionPane.showMessageDialog(null, c);
			}
		
		
		public static void aIncludesMinus ()
		{
			String[] a1 = a.split("-");
		
		String SubA = a1[1].substring(1);
		int a2 = Integer.parseInt(SubA);
		double a3 = Math.cbrt(a2);
		double a4 = Math.pow(a3, 2);
				
			String b = String.format("The original equation is: %s\n" 
				+ "The factored equation is: (x - %.0f) (x^2 + %.0fx + %.0f", a, a3, a3, a4);
			JOptionPane.showMessageDialog(null, b);
			
			String c = String.format("The root of the equation is: x = %.0f", a3);
			JOptionPane.showMessageDialog(null, c);
			}
		
		
}
